package simulation;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class AssemblyFactory extends Factory {
    private static final String MASS_KEYWORD = "mass";
    private static final String SPRING_KEYWORD = "spring";
    private static final String MUSCLE_KEYWORD = "muscle";

    // mass IDs
    private Map<Integer, Mass> myMasses = new HashMap<Integer, Mass>();

    /**
     * Adds assemblies to the model.
     */
    @Override
    protected void loadObjects (Model model, Scanner line, String type) {
        if (MASS_KEYWORD.equals(type)) {
            model.add(massCommand(line));
        }
        else if (SPRING_KEYWORD.equals(type)) {
            model.add(springCommand(line));
        }
        else if (MUSCLE_KEYWORD.equals(type)) {
            model.add(muscleCommand(line));
        }
    }

    /**
     * Creates mass from formatted data.
     */
    private Mass massCommand (Scanner line) {
        int id = line.nextInt();
        double x = line.nextDouble();
        double y = line.nextDouble();
        double mass = line.nextDouble();
        Mass result;
        if (mass < 0) {
            result = new FixedMass(x, y, -1 * mass);
        }
        else {
            result = new Mass(x, y, mass);
        }
        myMasses.put(id, result);
        return result;
    }

    /**
     * Creates spring from formatted data.
     */
    private Spring springCommand (Scanner line) {
        Mass m1 = myMasses.get(line.nextInt());
        Mass m2 = myMasses.get(line.nextInt());
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        return new Spring(m1, m2, restLength, ks);
    }

    /**
     * Creates muscle from formatted data.
     */
    private Spring muscleCommand (Scanner line) {
        Mass m1 = myMasses.get(line.nextInt());
        Mass m2 = myMasses.get(line.nextInt());
        double restLength = line.nextDouble();
        double ks = line.nextDouble();
        double amplitude = line.nextDouble();
        return new Muscle(m1, m2, restLength, ks, amplitude);
    }

}
